/** Build a file-based Lucene inverted index.
 * 
 * @author Scott Sanner
 */

package search;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class FileIndexBuilderBasic {

	public Analyzer  _analyzer; 
	public String    _indexPath;
	public final static String IGNORE_WORDS_ABS = "./src/nlp/nicta/filters/stopwords.txt";
	IndexWriter _indexWriter;
	
	public CharArraySet stopWord() throws IOException {
        String line;
        List<String> stopWord = new ArrayList<String>();
        BufferedReader br = new BufferedReader(new FileReader(IGNORE_WORDS_ABS));
        while ((line = br.readLine()) != null) {
        	stopWord.add(line.trim().toLowerCase());
        }
        br.close();
        
        String[] stopWordArray = new String[stopWord.size()];
        stopWord.toArray(stopWordArray);
        
        return StopFilter.makeStopSet(Version.LUCENE_42, stopWordArray);

	}
	
	public FileIndexBuilderBasic(String index_path) throws IOException {		
		_analyzer = new EnglishAnalyzer(Version.LUCENE_42, stopWord());
	    // Configurate the index path
	    _indexPath = index_path;
		File indexDir = new File(_indexPath);
		Directory index = new SimpleFSDirectory(indexDir);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_42, _analyzer);
		config.setOpenMode(OpenMode.CREATE);
		_indexWriter = new IndexWriter(index, config);
	}
	
	public void addFile(String filePathName, HashSet<String> fileContent) {
		try {
			Document doc = new Document();
		    StringBuilder content = new StringBuilder();
		    for (String s : fileContent) {
		    	content.append(fileContent + "\n");
		    }
		    doc.add(new TextField("CONTENT", content.toString(), Field.Store.YES));
		    doc.add(new TextField("PATH", filePathName, Field.Store.YES));
		    _indexWriter.addDocument(doc);
		} catch (Exception e){
			e.printStackTrace(System.err);
		}		
	}
	
	public void close() {
		try {
			_indexWriter.close();
		} catch (Exception e){
			e.printStackTrace(System.err);
		}		
	}
	
	public static void main(String[] args) throws Exception {
		
	}

}
